package com.base.component.ai.platform.core.converter;

import com.gitee.magic.converter.StringConverterEditor;

/**
 * @author start 
 */
public class PromptResultConverterEditor extends StringConverterEditor  {

	public static String PREFIX_BEGIN="```json\n{";
	public static String PREFIX_END="}\n```";

	public PromptResultConverterEditor(Class<?> prototype) {
		super(prototype);
	}
	
	@Override
	public void restore(Object value) {
		if(value!=null) {
			setValue(getData(String.valueOf(value)));
		}
	}

	@Override
	public Object getSource() {
		return String.valueOf(converter());
	}
	
	public static String getData(String data) {
		if(data.indexOf(PREFIX_BEGIN)==0&&data.indexOf(PREFIX_END)!=-1) {
			return data.substring(PREFIX_BEGIN.length()-1,data.indexOf(PREFIX_END)+1);
		}
		return data;
	}
	
}
